package com.spring.factorybean_demo;

import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import java.util.Iterator;

/**
 * 注解方式创建bean，使用bean
 * url: https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/context/annotation/Configuration.html
 */
public class App2 {
	public static void main(String[] args) {
		AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
		context.register(MyBeanConfiguration.class);
		context.refresh();
		ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
		Iterator<String> iterator = beanFactory.getBeanNamesIterator();
		while (iterator.hasNext()){
			String next = iterator.next();
			System.out.println(next);
		}

		//使用FactoryBean
		CustomService customService = (CustomService) context.getBean("myFactoryBean");
		CustomService customService2 = (CustomService) context.getBean("myFactoryBean");
//		CustomService customService =  bean.getObject();
//		CustomService customService2 =  bean.getObject();
		Integer item = customService.getId();
		System.out.println(item);

		//修改isSingleton =false时,表示多例
		//修改isSingleton =true时,表示单例
		System.out.println(customService == customService2);

	}
}
